/*
 * Copyright © 2020 ctwing
 */
package scep.ctwing.cn.smartapp.job;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import scep.ctwing.cn.smartapp.SmartappApplication;

import java.util.concurrent.*;

/**
 * @author:dailm
 * @create at :2020/12/4 11:19
 */
@Component
public class SortJob {
    ExecutorService pool;

    public SortJob(){
        pool = new ThreadPoolExecutor(50, Integer.MAX_VALUE, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
    }

    @Scheduled(cron = "*/30 * * * * ? ")
    public void startSort(){
        if("TRUE".equals(SmartappApplication.isAuto)) {
            for (int i = 0; i < SmartappApplication.THREAD_NUM; i++) {
                pool.execute(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("-------------" + Thread.currentThread() + "--begin--------------------");
                        long s = System.currentTimeMillis();
                        int[] datas = GenerateData.getRandomData(SmartappApplication.DATA_LEN);
                        datas = BubbleSort.doSort(datas);
                        long e = System.currentTimeMillis();
                        System.out.println("-------------" + Thread.currentThread() + "--end cost:" + (e - s) + "millis.");
                    }
                });
            }
        }
    }

    public void doSortByHandle(){
        for (int i = 0; i < SmartappApplication.THREAD_NUM; i++) {
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("-------------" + Thread.currentThread() + "--begin--------------------");
                    long s = System.currentTimeMillis();
                    int[] datas = GenerateData.getRandomData(SmartappApplication.DATA_LEN);
                    datas = BubbleSort.doSort(datas);
                    long e = System.currentTimeMillis();
                    System.out.println("-------------" + Thread.currentThread() + "--end cost:" + (e - s) + "millis.");
                }
            });
        }
    }
}
